{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [],
   "source": [
    "from keras.datasets import mnist \n",
    "import random\n",
    "import numpy as np\n",
    "#准备数据\n",
    "(x_train_photo, y_train_photo), (x_test_photo, y_test_photo) = mnist.load_data() \n",
    "def make_data_instance(x_photo, y_photo):\n",
    "    n = random.randint(0,10)\n",
    "    label = y_photo\n",
    "    return x_photo,n,label\n",
    "test_data = [ make_data_instance(x,y)  for x,y in zip(x_test_photo, y_test_photo)]\n",
    "#处理数据\n",
    "def process_data(train_data):\n",
    "    x_photo,n,label = zip(*train_data)\n",
    "    x_photo = np.array(x_photo) \n",
    "    n  = np.array(n) \n",
    "    label = np.array(label) \n",
    "    return  x_photo,n,label\n",
    "test_photo,test_n,test_label = process_data(test_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'outputs': [[6.98017597], [2.05478215]]}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "inputs = {\n",
    "    \"signature_name\": 'photo_mutiply_number',\n",
    "    \"inputs\": {\n",
    "        \"photo\": test_photo[:2].reshape(2,28,28).astype(np.float).tolist(),\n",
    "        \"number\": np.array(test_n[:2]).reshape(2,1).tolist()\n",
    "    }\n",
    "}\n",
    "rs = requests.post(json=inputs, url='http://localhost:8501/v1/models/photo_mutiply_number:predict')\n",
    "print(rs.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tf-env",
   "language": "python",
   "name": "tf-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
